<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动画开关</title>
    <style>
        .offscreen {
            /* 隐藏样式 */
            display: none; 
        }

        .switch {
            position: relative; /* 相对定位 */
            display: inline-block;
            width: 40px;
            height: 20px;
            background: #bfbfbf;
            border-radius: 20px;
            transition: all 0.3s; /* 背景颜色切换时有过渡的效果 */
        }

        .switch::after {
            content: '';
            position: absolute;
            width: 18px;
            height: 18px;
            border-radius: 18px;
            background: #fff;
            top: 1px;
            left: 1px;
            transition: all 0.3s;
        }

        input[type="checkbox"]:checked+.switch::after {
            transform: translateX(20px);
        }

        input[type=":checkbox"]:checked+.switch::after {
            background: pink;
        }
    </style>
</head>
<body>
    <input type="checkbox" id="toggle" class="offscreen">
    <label for="toggle" class="switch"></label>
</body>
</html>